# %%
import re
from importlib import import_module
from pprint import pprint 
import time
# 这一句只是为了import common 在 sys.path.append之后
if True:
    import sys
    import os
    sys.path.append(os.path.abspath('.'))
    import common

print("初始化 driver")
common.init_driver()

cur_dir=os.path.dirname(__file__)
cur_dir_file_ls = os.listdir(cur_dir)
pattern=re.compile("test_\d+.py")

def p(subpath):
    return os.path.join(cur_dir,subpath)

import_list = list()
for ele in cur_dir_file_ls:
    if pattern.match(ele):
        #先排除是当前文件
        if os.path.samefile(p(ele),__file__):
            continue
        import_name = os.path.splitext(ele)[0]
        import_list.append(import_name)

# adb日志文件
if not os.path.exists("log"):
    os.mkdir("log")
log_dir = os.path.join("log","{}".format(cur_dir.split(os.path.sep)[-1]))
if not os.path.exists(log_dir):
    os.mkdir(log_dir)
def get_log_file_path(test_name):
    log_file_name = "{}.txt".format(test_name)
    return os.path.join(log_dir,log_file_name)

# 导入并执行
pprint({"importlibs":import_list})
for im in import_list:
    log_file = get_log_file_path(im)
    with common.adb_log_context(log_file):
        try:
            import_module(im)
            time.sleep(10)
        except Exception as e:
            print(e)
            continue